﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;

namespace prjMateriais
{
    public partial class frmConsultarValidade : Form
    {
        bool firstClick;
        string condicaoPeriodo = "";

        public frmConsultarValidade()
        {
            InitializeComponent();
            habilitarDatas();
            firstClick = true;
            limparFiltro();
        }

        private void cboMaterial_Click(object sender, EventArgs e)
        {
            if (firstClick)
            {
                DataTable dados = Conexao.getInstance.executeQuery(SqlCrud.getInstance.selectAll(ConstTabelas.MATERIAL));
                cboMaterial.DataSource = dados;
                cboMaterial.DisplayMember = "Nome";
                cboMaterial.ValueMember = ConstTabelas.MATERIAL_PK;
                firstClick = false;
            }
        }

        public void updateGrid()
        {
            List<string> tabelas = new List<string>();
            List<string> campos = new List<string>();

            tabelas.Add(ConstTabelas.MATERIAL);
            tabelas.Add(ConstTabelas.LOTE);

            campos.Add(ConstTabelas.MATERIAL + ".Nome  AS Material");
            campos.Add(ConstTabelas.LOTE + ".Nome AS Lote");
            campos.Add(ConstTabelas.LOTE + ".Data AS 'Data de Validade'");

            string condicao = "(" + ConstTabelas.LOTE + ".CodigoMaterial = " + cboMaterial.SelectedValue + ")" + " and ("
                              + ConstTabelas.LOTE + ".CodigoMaterial = " + ConstTabelas.MATERIAL + ".Codigo)"
                              + condicaoPeriodo;

            string sql = SqlCrud.getInstance.select(tabelas, campos, condicao);
            Console.WriteLine(sql);

            try
            {
                DataTable dados = Conexao.getInstance.executeQuery(sql);
                dtgrdvwGrid.DataSource = dados;
            }
            catch { }
        }

        private void setCondicaoData()
        {
            if (dtpDataDe.Enabled && dtpDataAte.Enabled)
            {
                dtpDataDe.CustomFormat = "MM/dd/yyyy"; // BETWEEN do sq só funciona neste formato de data
                dtpDataAte.CustomFormat = "MM/dd/yyyy";
                dtpDataDe.Format = DateTimePickerFormat.Custom;
                dtpDataAte.Format = DateTimePickerFormat.Custom;

                condicaoPeriodo = " and (" + ConstTabelas.LOTE + ".Data BETWEEN '" + dtpDataDe.Text + "' and '" + dtpDataAte.Text + "')";

                dtpDataDe.Format = DateTimePickerFormat.Short; //p/ o usuario ver no formato dd/mm/yyyy
                dtpDataAte.Format = DateTimePickerFormat.Short;

                updateGrid();
            }
        }

        private void dtpDataDe_ValueChanged(object sender, EventArgs e)
        {
            setCondicaoData();
        }

        private void dtpDataAte_ValueChanged(object sender, EventArgs e)
        {
            setCondicaoData();
        }

        private void cboMaterial_SelectedIndexChanged(object sender, EventArgs e)
        {
            chkbVencidos.Enabled = true;
            updateGrid();
        }

        private void chkbVencidos_CheckedChanged(object sender, EventArgs e)
        {
            
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            habilitarDatas();
        }

        private void habilitarDatas()
        {
            dtpDataDe.Enabled = checkBox1.Checked;
            dtpDataAte.Enabled = checkBox1.Checked;
            dtpDataDe.Value = System.DateTime.Today;
            dtpDataAte.Value = System.DateTime.Today;
        }

        private void checkBox1_Click(object sender, EventArgs e)
        {
            if (!checkBox1.Checked)
            {
                condicaoPeriodo = "";

            }
            else
            {
                setCondicaoData();
            }
            updateGrid();
        }
        private void limparFiltro()
        {
            cboMaterial.DataSource = null;
            condicaoPeriodo = "";
            checkBox1.Checked = false;
            habilitarDatas();
            dtgrdvwGrid.DataSource = null;
            firstClick = true;
        }

        private void btnLimparFiltro_Click(object sender, EventArgs e)
        {
            limparFiltro();
        }

        private void chkbVencidos_Click(object sender, EventArgs e)
        {
            if (chkbVencidos.Checked)
            {
                String hoje = System.DateTime.Today.ToString(("MM/dd/yyyy"));

                condicaoPeriodo = " and (" + ConstTabelas.LOTE + ".Data < '" + hoje + "')";
                checkBox1.Checked = false;
            }
            else
            {
                condicaoPeriodo = "";
            }
            updateGrid();
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
